package com.probiz.estore.product.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.product.StockNotify;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.StockNotifyDao;
import com.probiz.estore.product.model.StockNotifySearchCriteria;
import com.probiz.estore.product.service.ProductManager;
import com.probiz.estore.product.service.StockNotifyManager;
import com.probiz.estore.system.service.MsgSendManager;
import com.probiz.estore.system.util.MsgTemplateConstants;


/**
 * Manager implementation for StockNotify, responsible for business processing, and communicate between web and persistence layer.
 */
public class StockNotifyManagerImpl extends GenericManagerImpl<StockNotify> implements StockNotifyManager {

	private StockNotifyDao stockNotifyDao = null;
	private ProductManager productManager = null;
	private MsgSendManager msgSendManager = null;
	/**
	 * @param stockNotifyDao
	 *            the stockNotifyDao to set
	 */
	
	public void setStockNotifyDao(StockNotifyDao stockNotifyDao) {
		this.stockNotifyDao = stockNotifyDao;
	}
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = stockNotifyDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(StockNotify entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(StockNotify entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.StockNotifyManager#searchByCriteria(com.probiz.estore.product.model.StockNotifySearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
	public List<StockNotify> searchByCriteria(StockNotifySearchCriteria stockNotifySearchCriteria, SearchCriteria searchCriteria){
		return stockNotifyDao.searchByCriteria(stockNotifySearchCriteria, searchCriteria);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.StockNotifyManager#searchByCriteriaForController(com.probiz.estore.product.model.StockNotifySearchCriteria, com.probiz.estore.core.search.SearchCriteria)
	 */
    public List<StockNotify> searchByCriteriaForController(StockNotifySearchCriteria stockNotifySearchCriteria, SearchCriteria searchCriteria){
    	if (searchCriteria.getPageNo() < 1)  //如果是翻页过前，就拿第一页就可以。
        {
            searchCriteria.changePaging(1, searchCriteria.getPageSize());
        }
	    List<StockNotify> result = searchByCriteria(stockNotifySearchCriteria, searchCriteria);
	    if (result == null|| result.size() == 0 )
	    {
	        if (searchCriteria.getPageNo() > searchCriteria.getTotalPageCount()) //如果是翻页前，就拿最后一页就可以。
	        {
	            searchCriteria.changePaging(searchCriteria.getTotalPageCount(), searchCriteria.getPageSize());
	            result = searchByCriteria(stockNotifySearchCriteria, searchCriteria);
	        }
	    }
		return result;
    }
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.StockNotifyManager#sendStockNotify(com.probiz.estore.common.model.product.StockNotify)
	 */
	public void sendStockNotify(StockNotify stockNotify){
		stockNotify.setInformStatus(Constants.FLAG_TRUE);
		stockNotify.setInformTime(new Date()); 
		stockNotifyDao.save(stockNotify);
		try{
			sendMail(stockNotify);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.StockNotifyManager#sendMail(com.probiz.estore.common.model.product.StockNotify)
	 */
	public void sendMail(StockNotify stockNotify){
		Map model = new HashMap();
		model.put("stockNotify", stockNotify);
		model.put("productUri", productManager.getUriByProduct(stockNotify.getProduct()));
		//mailEngine.sendSimpleTemplateMail(configUtil.getStockNotifyEmailTemplate(), model, null, null, stockNotify.getEmail());
		String phone = "" ;
		if(stockNotify.getCustomer() != null){
			phone = stockNotify.getCustomer().getMobilephone();
		}
		msgSendManager.sendMsg(MsgTemplateConstants.MESSAGE_STOCKOUT_REGISTER, model, new String[]{stockNotify.getEmail()}, stockNotify.getCustomerId() == null ? null : new String[]{stockNotify.getCustomerId().toString()}, new String[]{stockNotify.getPhone() == null ? phone : stockNotify.getPhone()});
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.StockNotifyManager#getByCustomerId(java.lang.Integer)
	 */
	public List<StockNotify> getByCustomerId(Integer customerId){
		return this.listByProperty("customer.appuserId", customerId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.StockNotifyManager#countUninformStockNotifyTotal()
	 */
	public Long countUninformStockNotifyTotal(){
		return this.stockNotifyDao.countUninformStockNotifyTotal();
	}
	public void setMsgSendManager(MsgSendManager msgSendManager) {
		this.msgSendManager = msgSendManager;
	}
}
